package com.liu.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@MapperScan(value="com.liu.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryBean1")//配置扫描器扫描
//扫描 com.liu.mapper1包下的 @Mapper
public class MyBatisConfigOne {

    @Autowired
     @Qualifier("dsOne")
    DataSource dsOne;
    @Bean   //配置sqlSession实例
    SqlSessionFactory sqlSessionFactoryBean1 () throws Exception {
        SqlSessionFactoryBean  factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dsOne);
        return factoryBean.getObject();
    }
    @Bean   //配置SqlSessionTemplate，传进去 sqlSessionFactoryBean1实例
    SqlSessionTemplate  sqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBean1());
    }

}
